#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Q1mi"
# Date: 2018/10/25


#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Q1mi"
# Date: 2018/10/25

##爬取中国天气网未来40天的福建9大地区的天气数据
##http://www.weather.com.cn/weather40d/101230103.shtml

import requests
import re
import os
import pandas as pd
import json
import time
from datetime import datetime, timedelta
import pymssql
import requests
#from code import *


def weater_ch(x):
    '''

    :param x:
    :return:  数字转化为中文
    '''
    #已经确定的有：'00':'晴','01':'多云','02':'阴','07':'小雨','08':'中雨','09':'大雨','301':'雨'
    c1_dic = {'00': '晴', '01': '多云', '02': '阴', '03': '阵雨', '04': '雷阵雨', '05': '雷阵雨', '06': '暴雨', '07': '小雨',
              '08': '中雨', '09': '大雨', '301': '雨'}

    if len(x) >0:
        if x in c1_dic.keys():
            c1_w = c1_dic[x]
        else:
            c1_w = '多云'
    else:
        c1_w = x
    return c1_w

def weater_zh(c1,c2):
    '''

    :return:  结合白天和晚上的气象中文说明
    '''
    word = ''
    if not c2:
        if c1 != c2:
            word+=c1
            word+='转'
            word += c2
    else:
        word= c1
    return c1


def spider(address_st,now_save_time):
    now_time = time.strftime('%Y%m%d', time.localtime(time.time()))
    now_time_15 = (datetime.now() + timedelta(days=+39)).strftime("%Y%m%d")#14 未来15天的，，，39：未来40天
    month_list = [now_time[4:6]]
    weater_list = set()


    if  now_time_15[4:6] != now_time[4:6]:
        for x in range(int(now_time[4:6])+1, int(now_time_15[4:6])+1):
            month_list.append(str(x))

    for month in month_list:
        home_page_url = 'http://d1.weather.com.cn/calendar_new/2018/{}_2018{}.html?_=1536541421639'.format(address_st,month)
        headers = {"Accept": "*/*",
                   "Accept-Encoding": "gzip, deflate",
                   "Accept-Language": "zh-CN,zh;q=0.9",
                   'Connection': 'keep-alive',
                   'Cookie': 'f_city=%E7%A6%8F%E5%B7%9E%7C101230101%7C; vjuids=74c546de1.165b2f1c48f.0.8fb370cd5c77; UM_distinctid=165b31b2f39255-02c904974464eb-9393265-1fa400-165b31b2f3dbe4; vjlast=1536305514.1536541371.13; Hm_lvt_080dabacb001ad3dc8b9b9049b36d43b=1536305514,1536541371; Wa_lvt_1=1536305514,1536541372; Wa_lpvt_1=1536541372; Hm_lpvt_080dabacb001ad3dc8b9b9049b36d43b=1536541422',
                   "Host": 'd1.weather.com.cn',
                   'Referer': 'http://www.weather.com.cn/weather40d/101230103.shtml',
                   "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
                   }
        response = requests.get(url=home_page_url, headers=headers).content.decode('utf8')

        # 转化为字典格式
        reg = re.sub(r'var fc40 = ', '', response)
        context_list = json.loads(reg)


        for x in context_list:

            if x['date'] > now_time_15:
                break

            if now_time_15 >= x['date'] >= now_time:
                context_lis = []
                context_lis.append(address_st)
                context_lis.append(x['max'])  # 最高温
                context_lis.append(x['min'])  # 最低温
                context_lis.append(x.get('w1'))  # 预测气温情况
                context_lis.append(x.get('wd1'))  # 预测风力
                rain1 = x.get('rain1')
                rain2 = x.get('rain2')
                if rain1 == '':
                    context_lis.append(rain1)  # 预测最大降水量
                    context_lis.append(rain2)  # 预测最小降水量
                else:
                    if float(rain1) >= float(rain2):
                        context_lis.append(rain1)  # 预测最大降水量
                        context_lis.append(rain2)  # 预测最小降水量
                    else:
                        context_lis.append(rain2)  # 预测最大降水量
                        context_lis.append(rain1)  # 预测最小降水量
                w_c1 = x.get('c1')
                w_c2 = x.get('c2')
                context_lis.append(w_c1) #天气情况 --- 晴天天气
                context_lis.append(w_c2) #天气情况 --- 夜间天气
                if not x.get('w1'):
                    wd_zh = ''
                    w_c1_zh = weater_ch(str(w_c1))
                    if w_c2:
                        w_c2_zh = weater_ch(str(w_c1))
                        wd_zh= weater_zh(w_c1_zh,w_c2_zh)
                    else:
                        wd_zh = w_c1_zh
                    context_lis[3] = wd_zh
                context_lis.append(x['date'])  # 预测的时间---YBTM
                context_lis.append(time.strftime('%Y-%m-%d ', time.localtime(time.time())) + x['time'])  # 更新时间--RSTM
                context_lis.append(now_save_time)  # 保存入数据库时间---PSTM

                weater_list.add(tuple(context_lis))

    #排序
    weater_list_sort = list(weater_list)
    weater_list_sort.sort(key=lambda x: x[-3])


    return weater_list_sort


def sql_connect(weater_list):
    server = "119.29.146.108"
    user = "sa"
    password = "S3686-e342qq-3686e342"

    connect = pymssql.connect(server, user, password, "istrong_data_collection")  # 获取连接
    cursor = connect.cursor()  # 获取光标

    for x in weater_list:
        cursor.execute(
            "INSERT INTO LJ_FutureWeater(STNM,wd_max,wd_min,weater,fl,js_max,js_min,c1,c2,YBTM,RSTM,PSTM) VALUES {}".format(x)

        )

        # 你必须调用 commit() 来保持你数据的提交如果你没有将自动提交设置为true
        connect.commit()
    print('=============写入sql')



def main(address_dic):
    now_save_time = time.strftime('%Y-%m-%d %H:00:00', time.localtime())
    weater_list = spider(address_dic,now_save_time)

    print(weater_list)
    print('完成一次气象更新')
    #
    # while True:
    #
    #     time.sleep(3600)  # 停一个小时
    #     now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    #     now_s = time.strftime('%H', time.localtime())
    #     print('=============time')
    #     print(now_s)
    #     if  now_s =='01' or now_s =='07' or now_s =='13' or now_s == '19':
    #         now_save_time = time.strftime('%Y-%m-%d %H:00:00', time.localtime())
    #         for x in address_dic.items():
    #             weater_list = spider(x[0], x[1],now_save_time)
    #             sql_connect(weater_list)
    #             print('==============ok')
    #             print('{}====time'.format(x),now_time,now_save_time)
    #         print('完成一次气象更新')
    #         continue

from code import sql
if __name__ == '__main__':
    code_name_set = '福州'
    code_name_out = '上海'
    time_1 = '20181026'
    time_2 = '20181027'

    #根据用户的出行名称
    select_sql_set = 'select code from StnmCode where STNM= \'{}\''.format(code_name_set)
    select_sql_out = 'select code from StnmCode where STNM= \'{}\''.format(code_name_out)

    code_set = sql.Sql(select_sql_set)
    code_out = sql.Sql(select_sql_out)
    print(code_set)
    #main(code_set)


